//销售合同履约状况
$(function () {
    //客户加载
    $("#xshtlyzk_kh_id").qyjgzcombobox();
    $("#grid_xshtlyzk").datagrid({
        view:detailview,
        detailFormatter:function (index, row) {
            return '<div class="detail" style="margin:8px 1px;border: 1px solid #A2D4E0"><table id="ddv-' + index + '"></table></div>';
        },
        onExpandRow:function (index, row) {
            //先要关闭其他Detail
            var indexes = $('#grid_xshtlyzk').datagrid('getRows').length;
            for (var i = 0; i < indexes; i++) {
                if (i != index)
                    $(this).datagrid("collapseRow", i);
            }
            var $this = $(this);
            if(parseFloat(row.yxsl) <= 0) {
                $("#grid_xshtlyzk").datagrid("collapseRow", i);
                return;
            }
            row.status = 1;
            $("#ddv-" + index).datagrid({
                url: $.webPath() + "/report/xshtlyzk/xshtlyzk_detail_list?htId=" + row.id,
                toolbar: "#grid_xshtlyzk_detail",
                fitColumns: true,
                singleSelect: true,
                fit: true,
                striped: true,
//                rownumbers: true,
                showFooter: true,
                height:'auto',
                columns: [
                    [
                        {field: 'xsdh', title: '销售单号', align: 'center', width: 100},
                        {field: 'gg.code', title: '物资编码', align: 'center', width: 100},
                        {field: 'gg.name', title: '物资名称', align: 'center', width: 100},
                        {field: 'gg.ggxh', title: '规格型号', align: 'center', width: 100},
                        {field: 'cksl', title: '出库数量', align: 'center', width: 100, formatter: function (value) {
                            var val = formatSlForHtml(value);
                            return val;
                        }},
                        {field: 'shj', title: '赎货价', align: 'center', width: 100, formatter: function (value) {
                            var val = formatSlForHtml(value);
                            return val;
                        }},
                        {field: 'kpzt', title: '开票', align: 'center', width: 100, formatter: function (value,r,i) {
                            if(r.xsdh)
                            if(value == 1)
                                return "已开票";
                            else
                                return "未开票";
                        }}
                    ]
                ],
                onResize:function(){
                    $("#grid_xshtlyzk").datagrid('fixDetailRowHeight',index);
                },
                onLoadSuccess:function(){
                    setTimeout(function(){
                        var height = $("#ddv-" + index).parent().height();
                        height = height > 200 ? 200 : height;
                        $("#ddv-" + index).parent().parent().height(height);
                        $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
                        $("#grid_xshtlyzk").datagrid('fixDetailRowHeight',index);
                    },0);
                }
            });
//            //打开本行
//            var ddv = $(this).datagrid('getRowDetail', index).find('div.detail');
//
//            var url = $.webPath() + "/report/xshtlyzk/xshtlyzk_detail?htId="+row.id+"&xsht="+row.xsht;
//            ddv.panel({
//                border:false,
//                cache:true,
//                href:url,
//                onLoad:function () {
//                    $('#grid_xshtlyzk').datagrid('fixDetailRowHeight', index);
//                    $('#grid_xshtlyzk').datagrid('selectRow', index);
//                }
//            });
            $('#grid_xshtlyzk').datagrid('fixDetailRowHeight', index);
        },
        onCollapseRow:function (index, row) {
            row.status = 0;
            if (!row.id)
                $(this).datagrid("deleteRow", index);
        },
        url: $.webPath() + "/report/xshtlyzk/xshtList",
        toolbar: "#toolbar_xshtlyzk",
        fitColumns: true,
        singleSelect: true,
        fit: true,
        pageNumber: 1,
        striped: true,
        pagination: true,
//        rownumbers: true,
//        height:'auto',
        columns: [
            [
                {field: 'cght', title: '采购合同', align: 'center', width: 100},
                {field: 'xsht', title: '销售合同', align: 'center', width: 100,formatter:xshtCell},
                {field: 'htq', title: '合同期（天）', align: 'center', width: 100},
                {field: 'htsl', title: '销售合同数量', align: 'center', width: 100, formatter: function (value) {
                    var val = formatSlForHtml(value);
                    return val;
                }},
                {field: 'yxsl', title: '已销数量', align: 'center', width: 100, formatter: function (v,r,i) {
                    var color = "#0000ff;";
                    if (v && v > 0) {
                        return '<a href="javascript:void(0)" style="color:' + color + '" class="xshtlyzk" htid="'+ r.id+'" index="'+ i +'">' + formatSlForHtml(v); + '</a>';
                    } else {
                        return v;
                    }
                }},
                {field: 'wxsl', title: '未销售数量', align: 'center', width: 100, formatter: function (value) {
                    var val = formatSlForHtml(value);
                    return val
                }},
                {field: 'wkpsl', title: '销售合同的执行状态', align: 'center', width: 100, formatter: function (v,r,i) {
                    if(parseFloat(r['wkpsl']) <= 0)
                        return "执行完毕";
                    else
                        return "未执行完毕";
                }}
            ]
        ],
        onDblClickRow:function(index,row){
            if (row){
                if(row.status=='0' || row.status==0){
                    $(this).datagrid('expandRow', index);
                    row.status=1;
                }else{
                    row.status=0;
                    $(this).datagrid('collapseRow', index);
                }
            }
        },
        onLoadSuccess: function () {
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
            $(".xshtlyzk").unbind("click").bind("click", function () {
                var index = $(this).attr("index");
                $("#grid_xshtlyzk").datagrid("expandRow",index);
//                var win = $.window({
//                    title: "销售合同已销数量明细",
//                    width: 800,
//                    height: 500,
//                    maximizable: true,
//                    cache: false,
//                    iconCls: 'icon-tip',
//                    url: $.webPath() + "/report/xshtlyzk/xshtlyzk_detail?htId="+$(this).attr("id")+"&xsht="+$(this).attr("xsht") + "&htbm=" + $(this).attr("htbm"),
//                    modal: true,
//                    buttons: [],
//                    onMaximize: function () {
//                        $(this).find("#xshtlyzk_detail_grid").edatagrid("resize");
//                    },
//                    onRestore: function () {
//                        $(this).find("#xshtlyzk_detail_grid").edatagrid("resize");
//                    }
//                });
            });
        }
    });

    $("#btn_xshtlyzk_search").click(function () {
        xshtSearch();
    });
});
function xshtSearch() {
    $('#grid_xshtlyzk').datagrid('load',
        {
            xsht:$('#xshtlyzk_xsht').val(),
            wzbm:$('#xshtlyzk_wzbm').val(),
            khId:$('#toolbar_xshtlyzk [name="xshtlyzk.kh.id"]').val(),
            cght:$("#xshtlyzk_cght").val(),
            xsdh:$("#xshtlyzk_xsdh").val()
        }
    );
}